package com.xunshi.hikangvision.vo;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.atomic.AtomicReference;

public class MyBlockingQueue {
    //  static public BlockingQueue bq = new ArrayBlockingQueue<>(10);
//    public  String socketSessionId;   //前端和这个摄像头建立的websocketid
//    public  String lUserID="-1";  //摄像头登录句柄
//    public  String lPlayID="-1";    //摄像预览句柄

    static public Map<String,String> SessionToUserIdMap=new HashMap<>();
    static public Map<String,String> PlayToUserIdMap=new HashMap<>();
    static public Map<String,String> IPToUserIdMap=new HashMap<>();
    static public Map<String,BlockingQueue> bpMap=new HashMap<>();

    /**
     * 通过摄像头登录句柄进行数据清理
     * @param userId
     */
    static public void clearByUserId(String userId,boolean logout)
    {
        if(null==MyBlockingQueue.bpMap)
        {
            System.out.println("bpMap is null ");
            MyBlockingQueue.bpMap=new HashMap<>();
        }
        if(null==MyBlockingQueue.PlayToUserIdMap)
        {
            System.out.println("PlayToUserIdMap is null ");
            MyBlockingQueue.PlayToUserIdMap=new HashMap<>();
        }
        if(null==MyBlockingQueue.SessionToUserIdMap)
        {
            System.out.println("SessionToUserIdMap is null ");
            MyBlockingQueue.SessionToUserIdMap=new HashMap<>();
        }

        for (String key : MyBlockingQueue.PlayToUserIdMap.keySet())
        {
            String value = MyBlockingQueue.PlayToUserIdMap.get(key);
            if(value.equals(userId))
            {
                MyBlockingQueue.PlayToUserIdMap.remove(key);
                break;
            }
        }
        for (String key : MyBlockingQueue.SessionToUserIdMap.keySet())
        {
            String value = MyBlockingQueue.SessionToUserIdMap.get(key);
            if(value.equals(userId))
            {
                MyBlockingQueue.SessionToUserIdMap.remove(key);
                break;
            }
        }
        if(logout)
        {
            for (String key : MyBlockingQueue.IPToUserIdMap.keySet())
            {
                String value = MyBlockingQueue.IPToUserIdMap.get(key);
                if(value.equals(userId))
                {
                    MyBlockingQueue.IPToUserIdMap.remove(key);
                    break;
                }
            }
        }
    }


    /**
     * 通过ip查找是否正在登录或者预览
     * @param Ip
     */
    static public String findUserIdByIp(String Ip)
    {
        for (String key : MyBlockingQueue.IPToUserIdMap.keySet())
        {
            String value = MyBlockingQueue.IPToUserIdMap.get(key);
            if(key.equals(Ip))
            {
                return value;
            }
        }
        return null;
    }

    /**
     * 通过摄像头登录句柄查询当前用户是否正在预览
     * @param userId
     */
    static public String findPlayIdByUserId(String userId)
    {
        for (String key : MyBlockingQueue.PlayToUserIdMap.keySet())
        {
            String value = MyBlockingQueue.PlayToUserIdMap.get(key);
            if(value.equals(userId))
            {
                return key;
            }
        }
        return null;
    }



}
